home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13659 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: String assignments, please help (beginner)
  5. Date: 9 Apr 1996 09:10 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <9APR199609100931@erich.triumf.ca>
  9. References: <4ke05g$27q@soap.news.pipex.net>
  10. NNTP-Posting-Host: ftp.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4ke05g$27q@soap.news.pipex.net>, tone@dial.pipex.com (Tone) writes...
  14. >I've just been learning C for the last few weeks and have come across
  15. >a problem with assigning strings, which I will detail below.
  16. >(Sorry the posting is so long, I'm just trying to make sure you know
  17. >exactly what my problem is):
  18. >I was originally informed that the following was valid:
  19. >    char string1[30];
  20. >    string1 = "Test string";
  21.  
  22. NO, it's not valid.
  23. >This gave me an error on the second line although when declaring I am
  24. >able to use:
  25. >    char string1[30] = "Test string";
  26.  
  27. Yes, this is a special case when _declaring_ an array.
  28.  
  29. >so I arrived at the following:
  30. >    char *string1[30];
  31. >    *string1 = "Test string";
  32.  
  33. This declares an array of 30 pointers-to-char, and sets string1[0] to point to
  34. the string literal "Test String".  (Actually, I'm not certain that the
  35. assignment is valid...)
  36.  
  37. To assign (or copy) strings, you need to use the strcpy() function.  If you use
  38. '=' (or '==') with strings, you are dealing with the value of the pointers to
  39. the strings, not with the string contents.
  40.  
  41.  
  42. >The only other way I know of assigning a string is to create a loop
  43. >(e.g. a "for" loop) and assign each character of the string
  44. >individually and manually add the '\0' at the end. This seems
  45. >extremely laborious and I'm sure it can't be the best way. (Also it
  46. >may give similar "unexpected results"?)
  47.  
  48. Well, actually it is the only way.  Fortunately, the compiler vendors (and C
  49. standard writers) have taken pity on us, and supply many standard functions to
  50. handle strings - most have names starting with "str" - have a look in your 
  51. library documentation for full details.
  52.  
  53. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  54. Internet: bennett@triumf.ca         | of one another only when one can be
  55. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  56. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  57. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  58. or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.